/*
 * Main.java
 */
package gov.nist.ssd.swqb.gui;

import gov.nist.ssd.swqb.core.Display;
import gov.nist.ssd.swqb.core.exceptions.InvalidURIException;
import gov.nist.ssd.swqb.core.exceptions.TimeoutException;
import java.io.File;



import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class Main extends SingleFrameApplication {

    public final String LOGGERID = "swqb-logger";
    private String args[];

    private void storeArgs(String args[]) {
        this.args = args;
    }

    private String[] getArgs() {
        return this.args;
    }
    private MainView mainView;

    private MainView getDesktopApplicationView() {
        return mainView;
    }

    protected void analyzeArg() {
        // -- load data if path as arg 
        File myFolder = null;

        if (args.length == 1) {
            myFolder = new File(args[0]);
        } else {
            return;
        }

        if (!(myFolder.exists() && myFolder.isDirectory() && myFolder.canWrite())) {
            return;
        }



        try {
            Display display = new Display(myFolder.getAbsolutePath(),
                    getDesktopApplicationView().getPrefixManager());
            getDesktopApplicationView().getGraphView().setDisplay(display);
        } catch (TimeoutException ex) {
        } catch (IOException e) {
        } catch (InvalidURIException e) {
        }
        // -------------------------------------------
    }

    /**
     * At startup create and show the main frame of the application.
     */
    @Override
    protected void startup() {
        this.mainView = new MainView(this);

        this.analyzeArg();

        show(mainView);
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override
    protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of Main
     */
    public static Main getApplication() {
        return Application.getInstance(Main.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(Main.class, args);
    }

    @Override
    protected void initialize(String[] args) {
        storeArgs(args);
    }
}
